﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace Krile.Kernel.Control.Surface.MessageObjects
{
    public class Hyperlink : Text
    {
        protected override Font InternalFont
        {
            get
            {
                return new Font(base.InternalFont, FontStyle.Underline);
            }
        }

        public string Target { get; set; }

        public Hyperlink(string uri)
            : this(uri, uri) { }

        public Hyperlink(string uri, string linkText)
            : base(linkText)
        {
            Target = uri;
        }

        public Hyperlink(string uri, String linkText, Color forecolor)
            : base(linkText, forecolor)
        {
            Target = uri;
        }

        enum ActivateStates { Unactivate, Hover, Active };
        ActivateStates _activateState = ActivateStates.Unactivate;
        ActivateStates activateState
        {
            get { return _activateState; }
            set
            {
                _activateState = value;
                ParentForm.RefreshTL(false);
            }
        }

        public Color overrideForeColor = Color.Empty;
        public override Color ForeColor
        {
            get
            {
                if (overrideForeColor != Color.Empty)
                    return overrideForeColor;
                switch (activateState)
                {
                    case ActivateStates.Unactivate:
                        return Bridge.Config.DisplayConfig.LinkColor;
                    case ActivateStates.Hover:
                        return Bridge.Config.DisplayConfig.LinkHoverColor;
                    case ActivateStates.Active:
                        return Bridge.Config.DisplayConfig.LinkActiveColor;
                    default:
                        throw new InvalidOperationException();
                }
            }
            set
            {
                overrideForeColor = value;
                this.ParentForm.RefreshTL(false);
            }
        }

        public override string HelptipText
        {
            get
            {
                if (Target.Length > 30)
                    return ">" + Target.Substring(0, 30) + "...";
                else
                    return ">" + Target;
            }
        }

        public override void OnMouseEnter(object sender, ChainMouseEventArgs e)
        {
            this.activateState = ActivateStates.Hover;
            ParentForm.SetCursor(Cursors.Hand);
        }

        public override void OnMouseLeave(object sender, ChainMouseEventArgs e)
        {
            this.activateState = ActivateStates.Unactivate;
            ParentForm.SetCursor(Cursors.Default);
        }

        public override void OnMouseDown(object sender, ChainMouseEventArgs e)
        {
            this.activateState = ActivateStates.Active;
        }

        public override void OnMouseUp(object sender, ChainMouseEventArgs e)
        {
            if (this.HitTest(e.Location))
                this.activateState = ActivateStates.Hover;
            else
                this.activateState = ActivateStates.Unactivate;
        }

        public override void OnMouseClick(object sender, ChainMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                Bridge.CommonOperation.CallBrowser(Target);
            else
                e.SetThrowSub();
        }
    }
}
